package com.example.controller;

import com.example.domain.dto.QuestionDTO;
import com.example.domain.po.AnswerRecord;
import com.example.domain.po.QuestionType;
import com.example.service.QuestionService;
import com.example.util.PageBean;
import com.example.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    /**
     * 分页获取题目
     * @return
     */
    @GetMapping("/list")
    public Result<PageBean<QuestionDTO>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) String topic, //题目
            @RequestParam(required = false) Integer type, //类型 0单选 1多选
            @RequestParam(required = false) Integer qtypeId //科室id
    ){
        return Result.success(questionService.list(pageNum,pageSize,topic,type,qtypeId));
    }


    /**
     * 随机获取50道题
     * @return
     */
    @GetMapping("/listByRandom")
    public Result<List<QuestionDTO>> listByRandom(){
        return Result.success(questionService.listByRandom());
    }

    /**
     * 记录用户交卷后的成绩以及时间
     * @param map 用户成绩和时间
     * @return
     */
    @PostMapping("/record")
    public Result<String> addRecord(@RequestBody Map<String, Object> map){
        questionService.record(map);
        return Result.success("记录完成");
    }

    /**
     * 通过uid查询最近用户的20条考试成绩
     * @param uid
     * @return
     */
    @GetMapping("/listRecordByUid")
    public Result<List<AnswerRecord>> listRecordByUid(Integer uid){
        return Result.success(questionService.listRecordByUid(uid));
    }

    /**
     * 根据考题分类id获取50道题目集
     * @param qTypeId
     * @return
     */
    @GetMapping("/listByQTypeId")
    public Result<List<QuestionDTO>> listByQTypeId(Integer qTypeId){
        return  Result.success(questionService.listByQTypeId(qTypeId));
    }

    /**
     * 获取所有考题分类
     * @return
     */
    @GetMapping("/listQuestionType")
    public Result<List<QuestionType>> listQuestionType(){
        return Result.success(questionService.listQuestionType());
    }

    /**
     * 通过id获取考题信息
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public Result<QuestionDTO> getById(Integer id){
        return Result.success(questionService.getById(id));
    }

    /**
     * 编辑考题
     * @param questionDTO
     * @return
     */
    @PostMapping("/edit")
    public Result<String> edit(@RequestBody QuestionDTO questionDTO){
        questionService.edit(questionDTO);
        return Result.success("修改成功");
    }

    /**
     * 新增考题
     * @param questionDTO
     * @return
     */
    @PostMapping("insert")
    public Result<String> insert(@RequestBody QuestionDTO questionDTO){
        System.out.println("111111111111111"+questionDTO);
        System.out.println("111111111111111"+questionDTO);
        System.out.println("111111111111111"+questionDTO);
        System.out.println("111111111111111"+questionDTO);
        questionService.insert(questionDTO);
        return Result.success("新增成功");
    }

    /**
     * 通过id删除考题
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById")
    public Result<String> deleteById(Integer id){
        questionService.deleteById(id);
        return Result.success("删除成功");
    }

    /**
     * 获取全部考题
     * @return
     */
    @GetMapping("/listAll")
    public Result<List<QuestionDTO>> listALl(){
        return Result.success(questionService.listAll());
    }

}
